package config

import (
	"context"
	"time"

	"github.com/zeromicro/go-zero/core/stores/postgres"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

func NewPostgre(database DataBase) sqlx.SqlConn {

	// if mysql: mysql := sqlx.NewMysql(mysqlConfig.DataSource)
	conn := postgres.New(database.DataSource)

	db, err := conn.RawDB()
	if err != nil {
		panic(err)
	}
	ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(database.ConnectionTimeout))
	defer cancel()
	err = db.PingContext(ctx)
	if err != nil {
		panic(err)
	}
	db.SetMaxOpenConns(database.MaxOpenConns)
	db.SetMaxIdleConns(database.MaxIdleConns)
	return conn
}
